﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TelerikedIn.Data;
using TelerikedIn.Models;
using TelerikedIn.WebApp.Areas.Administration.Models;

namespace TelerikedIn.WebApp.Areas.Administration.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class SkillsController : Controller
    {
        private readonly UowData data;

        public SkillsController()
            : this(new UowData())
        {
        }

        public SkillsController(UowData data)
        {
            this.data = data;
        }

        //
        // GET: /Administration/Skills/
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult TakeSkills([DataSourceRequest]
                                     DataSourceRequest request)
        {
            var skills = this.data.Skills.All().Select(SkillViewModel.FromSkill);
            return Json(skills.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        public JsonResult CreateSkill([DataSourceRequest]
                                      DataSourceRequest request, SkillViewModel skill)
        {
            if (CheckSkill(skill))
            {
                ModelState.AddModelError("Name", "Skill exist!");
            }

            if (skill != null && ModelState.IsValid)
            {
                var newSkill = new Skill
                {
                    Name = skill.Name
                };

                this.data.Skills.Add(newSkill);
                this.data.SaveChanges();

                skill.Id = newSkill.Id;
            }

            return Json(new[] { skill }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateSkill([DataSourceRequest]
                                      DataSourceRequest request, SkillViewModel skill)
        {
            if (CheckSkill(skill))
            {
                ModelState.AddModelError("Name", "Skill exist!");
            }

            if (skill != null && ModelState.IsValid)
            {
                var existSkill = this.data.Skills.All().FirstOrDefault(s => s.Id == skill.Id);
                existSkill.Name = skill.Name;
                this.data.SaveChanges();
            }

            return Json((new[] { skill }.ToDataSourceResult(request, ModelState)), JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteSkill([DataSourceRequest]
                                      DataSourceRequest request, SkillViewModel skill)
        {
            var existSkill = this.data.Skills.All().FirstOrDefault(s => s.Id == skill.Id);

            if (existSkill != null)
            {
                List<ApplicationUser> usersWithSkill = data.Users.All().Where(u => u.Skills.FirstOrDefault(s => s.Id == existSkill.Id) != null).ToList();
                for (int i = 0; i < usersWithSkill.Count(); i++)
                {
                    usersWithSkill[i].Skills.Remove(existSkill);
                }

                this.data.Skills.Delete(existSkill);
                this.data.SaveChanges();
            }

            return Json(new[] { skill }, JsonRequestBehavior.AllowGet);
        }

        private bool CheckSkill(SkillViewModel skill)
        {
            var existSkill = this.data.Skills.All().FirstOrDefault(s => s.Name == skill.Name);
            if (existSkill != null)
            {
                return true;
            }

            return false;
        }
    }
}